package com.epam.training;

import java.security.Principal;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.epam.training.domain.Customer;
import com.epam.training.formvalidate.RegisterForm;
import com.epam.training.service.user.UserService;

@Controller
public class SaveCustomerController {
	private final UserService service;

	@Autowired
	public SaveCustomerController(UserService service) {
		this.service = service;
	}

	// @ModelAttribute("customer")
	// private Customer createCustomer(@RequestParam String cLogin,
	// @RequestParam String cPassword,
	// @RequestParam(required = false) String cName,
	// @RequestParam(required = false) String cSurname,
	// @RequestParam(required = false) String cSecondname,
	// @RequestParam(required = false) String cDriverLicense,
	// @RequestParam String cEmail, @RequestParam String cPhone) {
	// Customer customer = new Customer();
	//
	// customer.setLogin(cLogin);
	// customer.setPassword(cPassword);
	// customer.setName(cName);
	// customer.setSurname(cSurname);
	// customer.setSecondName(cSecondname);
	// customer.setDriveLicense(cDriverLicense);
	// customer.setEmail(cEmail);
	// customer.setUserType("customer");
	// customer.setTelephone(cPhone);
	//
	// return customer;
	// }

	@RequestMapping(value = "/admin/addCustomer", method = RequestMethod.POST)
	private String addNewCustomer(Model model,
			@ModelAttribute @Valid Customer customer, BindingResult result) {

		if (result.hasErrors()) {
			model.addAttribute("error", "field.error");
			return "redirect:/admin/add/user";
		}

		service.save(customer);

		return AdminPageController.ADMIN_PAGE;
	}

	@RequestMapping(value = "/customer/update", method = RequestMethod.POST)
	private String updateCustomer(@ModelAttribute Customer customer,
			Principal principal) {

		service.update(customer, principal.getName());

		return "redirect:/customer";
	}

	@ModelAttribute("registerForm")
	public RegisterForm getRegisterForm() {
		return new RegisterForm();
	}

	public Customer createCustomer(RegisterForm form) {
		Customer customer = new Customer();
		customer.setLogin(form.getLogin());
		customer.setPassword(form.getPassword());
		customer.setEmail(form.getEmail());
		customer.setTelephone(form.getPhone());

		return customer;
	}

	@RequestMapping(value = "/createAccount")
	private String createAccount(Model model,
			@ModelAttribute("registerForm") @Valid RegisterForm form,
			BindingResult result) {

		if (result.hasErrors()) {
			return "register";
		}

		if (!form.getPassword().equals(form.getRepeatPassword())) {
			model.addAttribute("passwordMessage", "notMatchPassword");
		}

		service.save(createCustomer(form));
		model.addAttribute("message", "page.user.added");

		return "redirect:login";
	}
}
